/**
 * Created by shidaiapp on 2017/9/29.
 */

import actionUrls from "../../../../api/ApiUrls";
import ApiBase from "../../../../api/moudles/ApiBase";
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  //resetForm(self, tiyData);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.firstName = '';
  tiyData.tParams.contactEmail = '';
  tiyData.tParams.startEndDate = [];
  tiyData.tParams.orderBy = '';
  tiyData.tData.loginTypeDict = self.loginTypeDict;
  tiyData.tData.couponDefineList.splice(0, tiyData.tData.couponDefineList.length);
  tiyData.tParams.selectedCouponDefines.splice(0, tiyData.tParams.selectedCouponDefines.length);
};



/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiUser.verifyingUser({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "firstName": tiyData.tParams.firstName,
    "contactEmail": tiyData.tParams.contactEmail,
    "startDate": tiyData.tParams.startEndDate[0],
    "endDate": tiyData.tParams.startEndDate[1],
    "orderBy": tiyData.tParams.orderBy
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.userList.splice(0,tiyData.tData.userList.length);
      tiyData.tData.selectedItems.splice(0,tiyData.tData.selectedItems.length);
      let userPage = response.result;
      tiyData.tPage.total = userPage.total;
      tiyData.tPage.stepNumber = userPage.pageSize;
      userPage.list.forEach(
        function (item) {
          tiyData.tData.userList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 查看用户的申请信息
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const showVerifyUser = (self, tiyData,index) => {
  self.$Spin.show();
  tiyData.tParams.selectedCouponDefines.splice(0, tiyData.tParams.selectedCouponDefines.length);
  let userInfo = tiyData.tData.userList[index];
  self.$apiUser.userVerifyingInfo({
    "userId": userInfo.id,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      var userinfo = response.result;
      if(null == userinfo || userinfo.user == null){
        self.$Modal.warning({
          title:"确认",
          content:"用户申请信息不存在"
        });
        return;
      }
      tiyData.tData.userExtendInfo.id = userinfo.user.id;
      tiyData.tData.userExtendInfo.name = userinfo.user.name;
      tiyData.tData.userExtendInfo.facebook = userinfo.extendInfo.facebook;
      tiyData.tData.userExtendInfo.whatsapp = userinfo.extendInfo.whatsapp;
      tiyData.tData.userExtendInfo.twitter = userinfo.extendInfo.twitter;
      tiyData.tData.userExtendInfo.instagram = userinfo.extendInfo.instagram;
      tiyData.tData.userExtendInfo.source = userinfo.extendInfo.source;
      tiyData.tData.userExtendInfo.inviteCode = userinfo.extendInfo.inviteCode;
      tiyData.tData.userExtendInfo.useType = userinfo.extendInfo.useType;
      tiyData.tData.userExtendInfo.useContent = userinfo.extendInfo.useContent;
      tiyData.tData.userExtendInfo.createtime = userinfo.extendInfo.createtime;
      tiyData.tData.userExtendInfo.isPushMessage = userinfo.user.isPushMessage;
      tiyData.tData.userExtendInfo.phoneCode = userinfo.user.mobile;
      tiyData.tData.userExtendInfo.refusedReason = userinfo.extendInfo.refusedReason;
      // var way = '邮箱登录';
      // self.loginTypeDict.forEach(function (item) {
      //   if (item.value == userInfo.thirdParty) {
      //     way = item.name;
      //   }
      // });
      // tiyData.tData.userExtendInfo.loginWay = way;
      tiyData.tData.userExtendInfo.contactEmail = userInfo.contactEmail;
      tiyData.tParams.verifyInfo.userId = userInfo.id;
      tiyData.tParams.verifyInfo.userName = userInfo.name;
      tiyData.tStatus.modalUserInfo.isShow = true;
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
};
/**
 * 审核用户的申请信息
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const verifyUser = (self, tiyData) => {
  self.$Spin.show();
  let data = {
    "userId": tiyData.tParams.verifyInfo.userId,
    "userName": tiyData.tParams.verifyInfo.userName,
    "state": tiyData.tParams.verifyInfo.state,
    "refuseReason": tiyData.tParams.verifyInfo.refusedReason
  };
  if(data.state == 100 && tiyData.tParams.selectedCouponDefines.length >= 0){ //审核通过时发放vip优惠券
    let couponDefineIds = [];
    tiyData.tParams.selectedCouponDefines.forEach(function(item){
      couponDefineIds.push(item.id);
    });
    data.couponDefineIds = couponDefineIds;
  }

  self.$apiUser.verifyDone(data).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tStatus.modalUserInfo.isShow = false;
      webList(self, tiyData);
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
};

export const batchPass = (self, tiyData) => {
    self.$Spin.show();
    let data = {
      "userIds": tiyData.tData.selectedItems
    };
    if(tiyData.tParams.selectedCouponDefines.length >= 0){ //审核通过时发放vip优惠券
      let couponDefineIds = [];
      tiyData.tParams.selectedCouponDefines.forEach(function(item){
        couponDefineIds.push(item.id);
      });
      data.couponDefineIds = couponDefineIds;
    }
    self.$apiUser.batchVerifyDone(data).then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        tiyData.tStatus.modalBatchVerfiy.isShow = false;
        self.$Message.success("批量处理成功");
        webList(self, tiyData);
      }else{
        self.$Message.error(response.errorMessage);
      }
    });
};

/**
 * 查询VIP优惠券模版列表
 * @param self
 * @param tiyData
 */
export const getVipDefines = (self, tiyData) => {
  // self.$Spin.show();
  self.$apiCoupon.getCouponModelList({
    "validityPeriodType":2, //查询当月有效的优惠券模版
    "pageNum":0,
    "pageSize":20
  }).then(function (response){
    tiyData.tData.couponDefineList.splice(0, tiyData.tData.couponDefineList.length);
    self.$Spin.hide();
    if (response.success) {
      let couponPage = response.result;
      couponPage.list.forEach(
        function (item) {
          tiyData.tData.couponDefineList.push(item);
          if (item.state !== 0) {
            item._disabled = true;
          }
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
